package com.blb.network;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 聊天客户端
 */
public class ChatClient {

    //连接服务器
    public void connect(String ip,int port){
        //创建线程池
        int cpuNum = Runtime.getRuntime().availableProcessors() * 2;
        ExecutorService threadPool = new ThreadPoolExecutor(cpuNum,cpuNum,0, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>());
        //创建socket，连接服务器
        try{
            Socket socket = new Socket(ip,port);
            //启动读写线程
            threadPool.execute(new ReadRunnable(socket));
            threadPool.execute(new WriteRunnable(socket));
        }catch (IOException ex){
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new ChatClient().connect("192.168.114.22",8888);
    }
}
